package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.service.operator.WorkflowOperatorFieldHandler;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/18 16:27
 */
@Service("workflowOperatorLinkDataMultiHandlerImpl")
public class WorkflowOperatorLinkDataMultiHandlerImpl implements WorkflowOperatorFieldHandler {

    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.LINK_DATA_MULTI.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity,
                                       WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo) throws XbbException {
        return data.get(fieldAttrEntity.getAttr());
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity,
                                         FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo,
                                         WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        if (Objects.nonNull(workflowDataPojo.getValue())) {
            if (Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.LINK_DATA.getType())) {
                JSONArray multiData = new JSONArray();
                multiData.add(workflowDataPojo.getValue());
                return multiData;
            }
            return workflowDataPojo.getValue();
        } else {
            if (Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.LINK_DATA.getType())) {
                JSONArray multiData = new JSONArray();
                multiData.add(data.get(fieldAttrEntity.getAttr()));
                return multiData;
            }
            return data.get(fieldAttrEntity.getAttr());
        }
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        return null;
    }

    @Override
    public Object formulaHandlerThisField(FieldAttrEntity fieldAttrEntity, PaasFormDataEntity formDataEntity, JSONObject data, Long formId, Integer businessType, String corpid)  throws XbbException {
        return data.get(fieldAttrEntity.getAttr());
    }

    @Override
    public Object formulaHandlerTargetField(Object value, WorkflowOperationsPojo workflowOperationsPojo, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, String corpid) throws XbbException {
        if (Objects.isNull(value)) {
            return null;
        }
        // 等于关联数据
        if (value instanceof Number || value instanceof String) {
            JSONArray multiData = new JSONArray();
            multiData.add(Long.parseLong(value.toString()));
            return multiData;
        }
        // 等于关联数据多选
        if (value instanceof List || value.getClass().isArray()) {
            return JSON.parseArray(JSON.toJSONString(value), Long.class);
        }
        return null;
    }
}
